﻿using ExcelDataReader;
using Net.Event;
using Net.Table;
using System.Data;

namespace ExcelTools
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }

        public static void GenerateExcelData(string excelPath, string tablePath)
        {
            var path = tablePath + "/GameConfig.bytes";
            if (!Directory.Exists(tablePath))
                Directory.CreateDirectory(tablePath);
            var dataSetNew = new DataSetInfo();
            foreach (var file in Directory.GetFiles(excelPath, "*.*"))
            {
                if (!file.EndsWith(".xls") && !file.EndsWith(".xlsx"))
                    continue;
                var fileName = Path.GetFileName(file);
                if (fileName.StartsWith("~$"))
                    continue;
                var temp = file + ".temp";
                File.Copy(file, temp, true);
                FileStream stream = null;
                try
                {
                    stream = new FileStream(temp, FileMode.Open, FileAccess.Read);
                    using (var reader = ExcelReaderFactory.CreateReader(stream))
                    {
                        var dataSet = reader.AsDataSet();
                        foreach (DataTable table in dataSet.Tables)
                        {
                            if (table.Columns.Count == 0) //空表
                                continue;
                            if (table.Rows.Count < 3) //空行
                                continue;
                            if (table.TableName == "Type") //类型定义
                                continue;
                            var dataTable = new DataTableInfo(table.TableName);
                            var columnTypes = new List<ITypeSolver>();
                            for (int i = 0; i < table.Columns.Count; i++)
                            {
                                var columnName = table.Rows[0][i].ToString();
                                var columnType = table.Rows[1][i].ToString();
                                if (string.IsNullOrEmpty(columnName) | string.IsNullOrEmpty(columnType))
                                    continue;
                                if (!TypeSolver.TryGetValue(columnType, out var typeSolver))
                                    continue;
                                var dataType = typeSolver.DataType;
                                if (dataType.IsEnum)
                                    dataTable.Columns.Add(new DataColumnInfo(columnName, typeof(int))); //由于枚举类型可能存在于热更新程序集，而表读取早于热更新程序集，所以会导致读取不到类型的问题
                                else
                                    dataTable.Columns.Add(new DataColumnInfo(columnName, dataType));
                                columnTypes.Add(typeSolver);
                            }
                            for (int x = 3; x < table.Rows.Count; x++)
                            {
                                var row = table.Rows[x];
                                var rowId = row[0].ToString();
                                if (string.IsNullOrEmpty(rowId))
                                    continue;
                                var dataRow = dataTable.NewRow();
                                dataTable.Rows.Add(dataRow);
                                for (int y = 0; y < columnTypes.Count; y++)
                                    dataRow[y] = columnTypes[y].As(row[y]);
                            }
                            dataTable.AcceptChanges();
                            dataSetNew.Tables.Add(dataTable);
                        }
                    }
                }
                finally
                {
                    stream?.Close();
                    File.Delete(temp);
                }
            }
            dataSetNew.AcceptChanges();
            var bytes = TableBinarySerialize.Serialize(dataSetNew);
            File.WriteAllBytes(path, bytes);
            NDebug.Log("生成表格数据完成! " + path);
        }
    }
}
